//
//  SZLSecondMarketViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/24.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLSecondMarketViewController.h"
#import <UIImageView+WebCache.h>
#import "MyCountNetwork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "VefityTool.h"

@interface SZLSecondMarketViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *fireTimeLab;
@property (weak, nonatomic) IBOutlet UILabel *priceLab;
@property (weak, nonatomic) IBOutlet UITextField *transField;
@property (weak, nonatomic) IBOutlet UITextField *rateField;

@end

@implementation SZLSecondMarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"编辑期货";
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:self.model.thumbnailUrl] placeholderImage:GetImage(@"person_avatar")];
    self.nameLab.text = self.model.productName;
    self.priceLab.text = [NSString stringWithFormat:@"总价:%.2f  数量:%zd",self.model.totalPrice,self.model.productCount];
    self.fireTimeLab.text = [NSString stringWithFormat:@"%@",self.model.endDate];
}
- (IBAction)sureClick:(UIButton *)btn
{
    [self.view endEditing:YES];
    if (self.transField.text.length<1) {
        [MBProgressHUD showError:@"请输入转让价格" toView:self.view];
        return;
    }
    if (![VefityTool isValidNumber:self.transField.text]) {
        [MBProgressHUD showError:@"请输入有效的数字" toView:self.view];
        return;
    }
    if (self.fireTimeLab.text.length<1) {
        [MBProgressHUD showError:@"请输入预期收益率" toView:self.view];
        return;
    }
    if (![VefityTool isValidNumber:self.rateField.text]) {
        [MBProgressHUD showError:@"请输入有效的小于1的数字" toView:self.view];
        return;
    }
    if ([self.rateField.text floatValue]>1.0) {
        [MBProgressHUD showError:@"预期收益率不能大于1" toView:self.view];
        return;
    }
    
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"estimateIncomeRate"] = self.rateField.text;
    parame[@"price"] = self.transField.text;
    parame[@"orderNo"] = self.model.orderNo;
    [self showWaiter];
    [MyCountNetwork putSecondMarketWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"成功放入二级市场" toView:self.view];
            if (self.successBlock) {
                self.successBlock();
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}


@end
